﻿' ReRemind
' Copyright (C) 2009, Avian Waves Software
' SEE LICENSE.TXT for software licensing information


Public Class Form1
  Public Declare Function ActivateService Lib "coredll" (ByVal lpszDevKey As String, ByVal dwClientInfo As Integer) As IntPtr
  Public Declare Function DeregisterService Lib "coredll" (ByVal hDevice As IntPtr) As Boolean
  Public Declare Function GetServiceHandle Lib "coredll" (ByVal szPrefix As String, ByVal szDllName As String, ByVal pdwDllBuf As Integer) As IntPtr
  Public Declare Function NLedSetDevice Lib "coredll" (ByVal nDeviceId As Integer, ByRef pOutput As NLED_SETTINGS_INFO) As Boolean

  <Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential)> _
  Public Structure NLED_SETTINGS_INFO
    Dim LedNum As Integer
    Dim OffOnBlink As Integer
    Dim TotalCycleTime As Integer
    Dim OnTime As Integer
    Dim OffTime As Integer
    Dim MetaCycleOn As Integer
    Dim MetaCycleOff As Integer
  End Structure

  ' Workaround for smart devices that do not support the "button" object
  Friend ButtonChooseSound As Object

  Public Sub New()
    InitializeComponent()

    Try
      Me.ButtonChooseSound = New System.Windows.Forms.Button

      With CType(Me.ButtonChooseSound, System.Windows.Forms.Button)
        Dim scaleFactor As Integer = Me.AutoScaleDimensions.Width / 96
        .Location = New System.Drawing.Point(191 * scaleFactor, 203 * scaleFactor)
        .Name = "ButtonChooseSound"
        .Size = New System.Drawing.Size(25 * scaleFactor, 21 * scaleFactor)
        .TabIndex = 6
        .Text = "..."
      End With

      Me.Panel1.Controls.Add(CType(Me.ButtonChooseSound, System.Windows.Forms.Button))
      AddHandler CType(Me.ButtonChooseSound, System.Windows.Forms.Button).Click, AddressOf ButtonChooseSound_Click
    Catch ex As NotSupportedException
      ' Safe to ignore!  Smart devices do not support buttons
    End Try

    Me.Text = "ReRemind Config V7"
    Me.LabelTop.Text = "ReRemind V7" & vbCrLf & "Copyright 2009-2010, Avian Waves"
    Me.LoadRegistry()
  End Sub

  Private Sub LoadRegistry()
    Try
      NumericUpDownInterval.Value = CDec(GetRegValueInt("NotifyInterval", 15))
      CheckBoxUseDontNotifyDuring.Checked = GetRegValueBool("UseInactiveTime", False)
      NumericUpDownStart.Value = CDec(GetRegValueInt("HourInactiveStart", 0))
      NumericUpDownEnd.Value = CDec(GetRegValueInt("HourInactiveEnd", 0))
      CheckBoxNotifyReminder.Checked = GetRegValueBool("NotifyReminder", True)
      CheckBoxNotifyMissedCalls.Checked = GetRegValueBool("NotifyMissedCall", True)
      CheckBoxNotifyVoiceMail.Checked = GetRegValueBool("NotifyVoiceMail", True)
      CheckBoxNotifySMS.Checked = GetRegValueBool("NotifySMS", True)
      CheckBoxNotifyMMS.Checked = GetRegValueBool("NotifyMMS", True)
      CheckBoxNotifyEmail.Checked = GetRegValueBool("NotifyEmail", True)
      CheckBoxNotifyWhileAwake.Checked = GetRegValueBool("NotifyWhileOn", False)
      CheckBoxWakeUp.Checked = GetRegValueBool("PowerOnBeforePlay", False)
      NumericUpDownStopNotifying.Value = GetRegValueInt("StopNotifyAfter", 0)
      CheckBoxVibrate.Checked = GetRegValueBool("Vibrate", False)
      NumericUpDownVibrateLED.Value = GetRegValueInt("VibrateLEDIndex", 1)
      TextBoxSound.Text = GetRegValueString("SoundFile", "\Program Files\ReRemind\crystal.wav")
      CheckBoxAllDayHack.Checked = GetRegValueBool("AllDayWorkaroundHack", False)
    Catch ex As Exception
      MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
    End Try

    CheckBoxVibrate_CheckStateChanged(Me, EventArgs.Empty)
  End Sub

  Private Sub SaveRegistry()
    Try
      SetRegValueInt("NotifyInterval", CInt(NumericUpDownInterval.Value))
      SetRegValueBool("UseInactiveTime", CheckBoxUseDontNotifyDuring.Checked)
      SetRegValueInt("HourInactiveStart", CInt(NumericUpDownStart.Value))
      SetRegValueInt("HourInactiveEnd", CInt(NumericUpDownEnd.Value))
      SetRegValueBool("NotifyReminder", CheckBoxNotifyReminder.Checked)
      SetRegValueBool("NotifyMissedCall", CheckBoxNotifyMissedCalls.Checked)
      SetRegValueBool("NotifyVoiceMail", CheckBoxNotifyVoiceMail.Checked)
      SetRegValueBool("NotifySMS", CheckBoxNotifySMS.Checked)
      SetRegValueBool("NotifyMMS", CheckBoxNotifyMMS.Checked)
      SetRegValueBool("NotifyEmail", CheckBoxNotifyEmail.Checked)
      SetRegValueBool("NotifyWhileOn", CheckBoxNotifyWhileAwake.Checked)
      SetRegValueBool("PowerOnBeforePlay", CheckBoxWakeUp.Checked)
      SetRegValueInt("StopNotifyAfter", NumericUpDownStopNotifying.Value)
      SetRegValueBool("Vibrate", CheckBoxVibrate.Checked)
      SetRegValueInt("VibrateLEDIndex", NumericUpDownVibrateLED.Value)
      SetRegValueString("SoundFile", TextBoxSound.Text)
      SetRegValueBool("AllDayWorkaroundHack", CheckBoxAllDayHack.Checked)
    Catch ex As Exception
      MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
    End Try
  End Sub

  Private Sub ButtonChooseSound_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim sf As New SelectFile("*.WAV")
    If sf.ShowDialog = Windows.Forms.DialogResult.OK Then
      Me.TextBoxSound.Text = sf.FileName
    End If
  End Sub

  Private Sub MenuItemSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemSave.Click
    Me.SaveRegistry()

    Dim hsvc As IntPtr = GetServiceHandle("RER0:", Nothing, 0)
    If hsvc.ToInt32 <> -1 Then
      DeregisterService(hsvc)
    End If

    Threading.Thread.Sleep(100)

    ActivateService("ReRemindSvc", 0)

    Me.Close()
  End Sub

  Private Sub MenuItemCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemCancel.Click
    Me.Close()
  End Sub

  Private Sub CheckBoxVibrate_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxVibrate.CheckStateChanged
    NumericUpDownVibrateLED.Enabled = CheckBoxVibrate.Checked
    LabelVibrateLED.Enabled = CheckBoxVibrate.Checked
  End Sub

  Private Sub LinkLabel1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkLabel1.Click
    MessageBox.Show("In Windows Mobile 6.1 and newer, ""All Day"" reminders will not make a sound nor vibrate, even if your settings specify they should.  There is no fix from Microsoft.  ReRemind can work around this bug by playing the reminder sound (WAV only) and vibrating, just like your device is supposed to.  This workaround ONLY works for Windows Phone 6.5.  Additionally, AutoSnooze will still not work (two subsequent reminders if you don't dismiss the first).", "Workaround Explanation", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1)
  End Sub

  Private Sub MenuItemPlaySound_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemPlaySound.Click
    Dim snd As New Sound(Me.TextBoxSound.Text)
    snd.Play()
  End Sub

  Private Sub MenuItemVibe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemVibe.Click
    Me.LedOn(Me.NumericUpDownVibrateLED.Value)
    Threading.Thread.Sleep(1000)
    Me.LedOff(Me.NumericUpDownVibrateLED.Value)
  End Sub

  Private Sub LedOn(ByVal id As Integer)
    Dim settings As New NLED_SETTINGS_INFO
    settings.LedNum = id
    settings.OffOnBlink = 1
    NLedSetDevice(2, settings)
  End Sub

  Private Sub LedOff(ByVal id As Integer)
    Dim settings As New NLED_SETTINGS_INFO
    settings.LedNum = id
    settings.OffOnBlink = 0
    NLedSetDevice(2, settings)
  End Sub
End Class
